﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Marmat.Foundation;

namespace Marmat.Template.WinForm
{
    /// <summary>
    /// 基于DataTable的单据命令实现。
    /// </summary>
    public class DocumentCommand : IDocumentCommand
    {
        private readonly IDefaultDocumentCommand command;

        public DocumentCommand()
        {
            this.command = Marmat.FrameWork.SystemCommand.GetService<IDefaultDocumentCommand>();
            this.command.Command = this;
        }
        public virtual string Name => this.command.Name;

        public virtual string PrimaryName => this.command.PrimaryName;


        public virtual object Create(DataRow item)
        {
            return this.command.Create(item);
        }

        public virtual void Delete(DataRow item)
        {
            this.command.Delete(item);
        }

        public virtual DataTable FillSchema()
        {
            return this.command.FillSchema();
        }

        public virtual DataTable Load(string condition)
        {
            return this.command.Load(condition);
        }

        public virtual DataRow Load(object id)
        {
            return this.command.Load(id);
        }

        public virtual void Write(DataRow item)
        {
            this.command.Write(item);
        }
    }

    /// <summary>
    /// 基于对象的单据命令实现。
    /// </summary>
    public class DocumentCommand<Entity> : IDocumentCommand<Entity> where Entity : new()
    {
        private readonly IDefaultDocumentCommand<Entity> command;

        public DocumentCommand()
        {
            this.command = Marmat.FrameWork.SystemCommand.GetService<IDefaultDocumentCommand<Entity>>();
            this.command.Command = this;
        }

        public virtual string Name { get; }

        public virtual string PrimaryName { get; }

        public virtual object Create(Entity item)
        {
            return this.command.Create(item);
        }

        public virtual void Delete(Entity item)
        {
            this.command.Delete(item);
        }

        public virtual Entity[] Load(string condition)
        {
            return this.command.Load(condition);
        }

        public virtual Entity Load(object id)
        {
            return this.command.Load(id);
        }

        public virtual void Write(Entity item)
        {
            this.command.Write(item);
        }
    }
}
